﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Dextrys.SharePoint;
using Higer.SharePoint.HigerWebService;

namespace Higer.SharePoint.UserControls
{
    public partial class moreMyTask : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            try
            {
                //DataTable dt_MyTasks = GetMyTasks("zhouhx");
                DataTable dt_MyTasks = GetMyTasks(GetCurrentUserName(this.CurrentUser.LoginName));

                if (dt_MyTasks != null && dt_MyTasks.Rows.Count > 0)
                {
                    gvMyTask.DataSource = dt_MyTasks;
                    gvMyTask.DataBind();
                    gvMyTask.Visible = true;
                    lblMessage.Visible = false;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = ex.Message;
                gvMyTask.Visible = false;
            }

 


            //}
        }

        #region 属性

        #endregion

        #region 方法

        public string GetCurrentUserName(string loginName)
        {
            if (loginName.Contains('\\'))
            {
                loginName = loginName.Substring(loginName.IndexOf('\\') + 1);
            }
            return loginName;
        }

        public DataTable GetMyTasks(string username)
        {
            HigerWorkList higerWorkList = new HigerWorkList();
            DataSet ds = higerWorkList.GetMyTasks(username);
            if (ds!=null&&ds.Tables.Count>0)
            {
                return ds.Tables[0];
            }
            else
            {
                return null;
            }
            
        }        

        public string GetDate(string startdate, string enddate)
        {
            string date = "";
            try
            {
                DateTime dtStartDate = Convert.ToDateTime(startdate);
                startdate = dtStartDate.ToString("MM/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
                DateTime dtEndDate = Convert.ToDateTime(enddate);
                enddate = dtEndDate.ToString("MM/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            date = startdate + "~" + enddate;
            return date;
        }

        public string GetTaskTitle(string tasktitle, int length)
        {
            string resulttitle = "";

            resulttitle = GetStringByLength(tasktitle, length);

            return resulttitle;
        }

        /// <summary>
        /// 裁减字符串到指定的大小
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private string GetStringByLength(string str, int len)
        {
            bool fDBCSStarting = false;

            byte[] strArray = System.Text.Encoding.Default.GetBytes(str);
            if (strArray.Length <= len)
            {
                return str;
            }
            else
            {
                len = len - 4;
                byte[] ret = new byte[len + 1];
                for (int i = 0; i < len; i++)
                {
                    ret[i] = strArray[i];
                    // determine if the current byte is the starting byte of a DBCS character
                    if (ret[i].CompareTo((byte)0x7f) > 0)
                        fDBCSStarting = !fDBCSStarting;
                }


                if (fDBCSStarting == true || strArray[len].CompareTo((byte)0x80) < 0)
                    // read the ending byte
                    ret[len] = strArray[len];
                else
                    ret[len] = (byte)'.';

                return System.Text.Encoding.Default.GetString(ret) + "...";
            }
        }

        #endregion

        #region 事件

        protected void gvMyTask_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowIndex >= 0))
            {
                string startdate = e.Row.Cells[3].Text;
                string enddate = e.Row.Cells[4].Text;

                if (startdate != null && enddate != null)
                {
                    DateTime stratDate = Convert.ToDateTime(startdate.ToString());
                    DateTime endDate = Convert.ToDateTime(enddate.ToString());

                    if (stratDate.CompareTo(DateTime.Today) > 0)
                    {
                        e.Row.CssClass = "myblackcss";
                    }
                    else if (stratDate.CompareTo(DateTime.Today) <= 0 && endDate.CompareTo(DateTime.Today) >= 0)
                    {
                        e.Row.CssClass = "mygreencss";
                    }
                    else if (endDate.CompareTo(DateTime.Today) < 0)
                    {
                        e.Row.CssClass = "myredcss";
                    }

                    if (endDate.CompareTo(DateTime.Today) >= 0 && endDate.CompareTo(DateTime.Today.AddDays(2)) <= 0)
                    {
                        e.Row.CssClass = "mybluecss";
                    }
                }

            }

            // 隐藏任务开始时间
            e.Row.Cells[3].Visible = false;
            //隐藏任务结束时间
            e.Row.Cells[4].Visible = false;
        }

        #endregion
    }
}